import { api } from '@/api/request'
import type { PageData, PageQuery } from '@/types'

// 家庭信息模型
export interface FamilyInfo {
    id: number
    name: string
    description: string | null
    avatar: string | null
    is_active: boolean
    order: number
    parent_id: number
    max_members: number
    created_by: number
    created_at: string
    updated_at: string
}

// 家庭成员信息模型
export interface FamilyMemberInfo {
    id: number
    user_id: number
    family_id: number
    nickname: string | null
    avatar: string | null
    is_admin: boolean
    is_active: boolean
    joined_at: string
    permissions: string[]
    username: string // 用户名（关联用户表）
    email: string // 邮箱（关联用户表）
}

// 家庭详细信息（包含成员）
export interface FamilyDetail extends FamilyInfo {
    members: FamilyMemberInfo[]
    member_count: number
}

// 家庭查询参数
export interface FamilyQuery extends PageQuery {
    name?: string
    is_active?: boolean
    created_by?: number
    parent_id?: number
}

// 家庭成员查询参数
export interface FamilyMemberQuery extends PageQuery {
    family_id?: number
    user_id?: number
    is_admin?: boolean
    is_active?: boolean
}

// 创建家庭请求参数
export interface CreateFamilyRequest {
    name: string
    description?: string
    avatar?: string
    order?: number
    parent_id?: number
    max_members?: number
}

// 更新家庭请求参数
export interface UpdateFamilyRequest {
    name?: string
    description?: string
    avatar?: string
    is_active?: boolean
    order?: number
    parent_id?: number
    max_members?: number
}

// 添加家庭成员请求参数
export interface AddFamilyMemberRequest {
    user_id: number
    nickname?: string
    avatar?: string
    is_admin?: boolean
    permissions?: string[]
}

// 更新家庭成员请求参数
export interface UpdateFamilyMemberRequest {
    nickname?: string
    avatar?: string
    is_admin?: boolean
    is_active?: boolean
    permissions?: string[]
}

// 家庭API接口定义
export const FamilyApi = {
    // 家庭管理
    getFamilies: (params: FamilyQuery) => {
        return api.get<PageData<FamilyInfo>>('/family/family_member/list', { params })
    },
    
    getFamilyDetail: (id: number) => {
        return api.get<FamilyDetail>(`/family/family_member/${id}`)
    },
    
    createFamily: (data: CreateFamilyRequest) => {
        return api.post<FamilyInfo>('/family/family_member/create', data)
    },
    
    updateFamily: (id: number, data: UpdateFamilyRequest) => {
        return api.put<FamilyInfo>(`/family/family_member/${id}`, data)
    },
    
    deleteFamily: (id: number) => {
        return api.delete(`/family/family_member/${id}`)
    },

    // 家庭成员管理
    getFamilyMembers: (params: FamilyMemberQuery) => {
        return api.get<PageData<FamilyMemberInfo>>('/api/family-members', { params })
    },
    
    addFamilyMember: (id: number, data: AddFamilyMemberRequest) => {
        return api.post<FamilyMemberInfo>(`/family/family_member/${id}/members`, data)
    },

    // // 注意：根据你的注释，这个接口暂无
    // updateFamilyMember: (id: number, data: UpdateFamilyMemberRequest) => {
    //     return api.put<FamilyMemberInfo>(`/family/family-members/${id}`, data)
    // },
          
    removeFamilyMember: (id: number, user_id: number) => {
        return api.delete(`/family/family-members/${id}/members/${user_id}`)
    },
    
    // // 注意：根据你的注释，这个接口暂无
    // setAdmin: (user_id: number, is_admin: boolean) => {
    //     return api.put<FamilyMemberInfo>(`/family/family-members/${user_id}/admin`, { is_admin })
    // }
}